import 'shop.dart';
import 'home_comment.dart';

/// 首页附近商家响应模型（匹配安卓版本结构）
class HomeNearbyResponse {
  final String status;                    // 响应状态
  final String? message;                  // 响应消息（可选）
  final HomeComment? dataComment;         // 单个评论对象（匹配安卓版data_comment）
  final List<Shop> dataShop;              // 商店列表（匹配安卓版data_shop）
  final Map<String, dynamic>? pagination; // 分页信息

  HomeNearbyResponse({
    required this.status,
    this.message,
    this.dataComment,
    required this.dataShop,
    this.pagination,
  });

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      if (message != null) 'message': message,
      'data_comment': dataComment?.toJson(),
      'data_shop': dataShop.map((s) => s.toJson()).toList(),
      if (pagination != null) 'pagination': pagination,
    };
  }

  factory HomeNearbyResponse.fromJson(Map<String, dynamic> json) {
    // 解析单个评论数据（匹配安卓版本）
    HomeComment? comment;
    if (json['data_comment'] != null && json['data_comment'] is Map) {
      try {
        comment = HomeComment.fromJson(json['data_comment'] as Map<String, dynamic>);
      } catch (e) {
        print('解析data_comment失败: $e');
      }
    }
    
    // 解析商店数据
    List<Shop> shopList = [];
    if (json['data_shop'] != null && json['data_shop'] is List) {
      try {
        shopList = (json['data_shop'] as List)
            .map((shop) => Shop.fromJson(shop))
            .toList();
      } catch (e) {
        print('解析data_shop失败: $e');
      }
    }

    return HomeNearbyResponse(
      status: json['status'] as String? ?? 'error',
      message: json['message'] as String?,
      dataComment: comment,
      dataShop: shopList,
      pagination: json['pagination'] as Map<String, dynamic>?,
    );
  }

  /// 是否成功
  bool get isSuccess => status == 'success';
  
  /// 兼容性属性 - 获取评论列表
  List<HomeComment> get comments => dataComment != null ? [dataComment!] : [];
  
  /// 兼容性属性 - 获取商店列表
  List<Shop> get shops => dataShop;
  
  /// 为了兼容旧代码，提供data属性
  HomeNearbyData? get data {
    if (dataShop.isEmpty && dataComment == null) return null;
    return HomeNearbyData(
      shops: dataShop,
      comments: comments,
      categories: ['餐饮', '娱乐', '购物', '酒店', '其他'], // 默认分类
    );
  }

  @override
  String toString() {
    return 'HomeNearbyResponse(status: $status, comment: ${dataComment != null ? '1' : '0'}, shops: ${dataShop.length})';
  }
}

/// 首页附近数据模型
class HomeNearbyData {
  final List<Shop> shops;              // 商店列表
  final List<HomeComment> comments;    // 评论列表
  final List<String> categories;       // 分类列表
  final int? totalShops;               // 总商店数
  final int? totalComments;            // 总评论数
  final int? currentPage;              // 当前页码
  final int? totalPages;               // 总页数

  HomeNearbyData({
    required this.shops,
    required this.comments,
    required this.categories,
    this.totalShops,
    this.totalComments,
    this.currentPage,
    this.totalPages,
  });

  Map<String, dynamic> toJson() {
    return {
      'shops': shops.map((shop) => shop.toJson()).toList(),
      'comments': comments.map((comment) => comment.toJson()).toList(),
      'categories': categories,
      if (totalShops != null) 'total_shops': totalShops,
      if (totalComments != null) 'total_comments': totalComments,
      if (currentPage != null) 'current_page': currentPage,
      if (totalPages != null) 'total_pages': totalPages,
    };
  }

  factory HomeNearbyData.fromJson(Map<String, dynamic> json) {
    return HomeNearbyData(
      shops: json['shops'] != null
          ? (json['shops'] as List).map((shop) => Shop.fromJson(shop)).toList()
          : json['data_shop'] != null
              ? (json['data_shop'] as List).map((shop) => Shop.fromJson(shop)).toList()
              : [],
      comments: json['comments'] != null
          ? (json['comments'] as List).map((comment) => HomeComment.fromJson(comment)).toList()
          : json['data_comment'] != null && json['data_comment'] is List
              ? (json['data_comment'] as List).map((comment) => HomeComment.fromJson(comment)).toList()
              : [],
      categories: json['categories'] != null
          ? List<String>.from(json['categories'])
          : ['餐饮', '娱乐', '购物', '酒店', '其他'], // 默认分类
      totalShops: json['total_shops'] as int?,
      totalComments: json['total_comments'] as int?,
      currentPage: json['current_page'] as int?,
      totalPages: json['total_pages'] as int?,
    );
  }

  @override
  String toString() {
    return 'HomeNearbyData(shops: ${shops.length}, comments: ${comments.length}, categories: ${categories.length})';
  }
}
